/**
 * BoxSession
 * created at 2000-5-17
 * @author: Jianwen Zhang
 */

package linuxbox.common;

import java.util.*;
import org.apache.log4j.Logger;

import linuxbox.BoxStatic;
import linuxbox.admin.logic.UserRights;
import com.thebox.config.*;

public class BoxSession {
    private int m_iID;
    private String m_sLoginID;
    private String m_sUserName;
    private long m_lUserRights;
    private TimeZone m_TimeZone;

    protected Map m_settings = new HashMap(5);
    protected static Logger logger = Logger.getLogger(BoxSession.class.getName());

    static public int DST_ON = 0;
    static public int DST_OFF = 1;
    static public int DST_AUTO = 2;

    public BoxSession(int id, String loginID, String userName,
                      long userRights) {
        m_iID = id;
        m_sLoginID = loginID;
        m_sUserName = userName;
        m_lUserRights = userRights;
    }

    public int getID() {
        return m_iID;
    }

    public String getLoginID( ) {
        return m_sLoginID;
    }

    public String getUserName( ) {
        return m_sUserName;
    }

    public long getUserRights( ) {
        return m_lUserRights;
    }

    public boolean isAnyAdmin() {
        return UserRights.isAnyAdmin(m_lUserRights);
    }

    public boolean isNewsAdmin() {
        return UserRights.isNewsAdmin(m_lUserRights);
    }

    public boolean isPollAdmin() {
        return UserRights.isPollAdmin(m_lUserRights);
    }

    public boolean isResourceAdmin() {
        return UserRights.isResourceAdmin(m_lUserRights);
    }

    public boolean isReservationAdmin() {
        return UserRights.isReservationAdmin(m_lUserRights);
    }

    public boolean isDownloadAdmin() {
        return UserRights.isDownloadAdmin(m_lUserRights);
    }

    public boolean isBBSAdmin() {
        return UserRights.isBBSAdmin(m_lUserRights);
    }

    public boolean isUserAdmin() {
        return UserRights.isUserAdmin(m_lUserRights);
    }

    public boolean isSystemAdmin() {
        return UserRights.isSystemAdmin(m_lUserRights);
    }

    public boolean isTodoAdmin() {
        return UserRights.isTodoAdmin(m_lUserRights);
    }

    public boolean isBookmarkAdmin() {
        return UserRights.isBookmarkAdmin(m_lUserRights);
    }

    public TimeZone getTimeZone()
    {
      if (m_TimeZone == null)
      {
        String defaultTz = getConfigString("calendar", "Date & Time", "Time Zone");
        if (defaultTz == null)
          defaultTz = "GMT-08:00";
        int iDST = getConfigInt("calendar", "Date & Time", "DST");
        if (iDST == DST_ON)
        {
          StringBuffer tmp = new StringBuffer(9);
          tmp.append(defaultTz.substring(0,4));
          int iHour = Integer.parseInt(defaultTz.substring(3,6));
          iHour ++;
          if (iHour <0)
            iHour = -iHour;
          tmp.append(iHour);
          tmp.append(defaultTz.substring(6,9));
          defaultTz = tmp.toString();
        }
        m_TimeZone = TimeZone.getTimeZone(defaultTz);
      }
      return m_TimeZone;
    }

    public void setTimeZone(String tz)
    {
      setConfigString("calendar", "Date & Time", "Time Zone", tz);
      m_TimeZone = null;
      getTimeZone();
    }

    public String getConfigString(String module, String category,
                                  String name) {
      return getConfigString(module, category, name, true);
    }

    public String getConfigString(String module, String category,
                                  String name, boolean useDefault) {
        Map t = getConfigTable(module, category);
        String val = (String) t.get(name);
        if (val == null)
        {
          val = getConfigValue(module, category, name, useDefault);
          t.put(name, val);
        }
        return val;
    }

    public String[] getConfigStrings(String module, String category,
                                     String name) {
      return getConfigStrings(module, category, name, true);
    }
     // the useDefault parameter is used when user's config not exist, it indicats whether should get the config data from
    // the module as user's default config
    public String[] getConfigStrings(String module, String category,
                                     String name, boolean useDefault) {
        Map t = getConfigTable(module, category);
        String[] val = (String[]) t.get(name);
        if (val == null)
        {
          val = getConfigValues(module, category, name, useDefault);
          t.put(name, val);
        }
        return val;
    }

    public int getConfigInt(String module, String category,
                            String name) {
      return getConfigInt(module, category, name, true);
    }
    public int getConfigInt(String module, String category,
                            String name, boolean useDefault) {
        Map t = getConfigTable(module, category);
        String val = (String) t.get(name);
        if (val == null)
        {
          val = getConfigValue(module, category, name, useDefault);
          t.put(name, val);
        }
        return Integer.parseInt(val);
    }

    public boolean getConfigBoolean(String module, String category,
                                     String name) {
       return getConfigBoolean(module, category, name, true);
     }

    public boolean getConfigBoolean(String module, String category,
                                    String name, boolean useDefault) {
        Map t = getConfigTable(module, category);
        String val = (String)t.get(name);
        if (val == null)
        {
          val = getConfigValue(module, category, name, useDefault);
          // TODO: need further check
          if (val == null)
            return false;

          t.put(name,val);
        }

        return val.compareTo("0") != 0;
    }

    public boolean setConfigString(String module, String category,
                                   String name, String val) {
        Map t = getConfigTable(module, category);
        try {
            BoxConfigUtil.setUserConfigValue(m_iID, module, category,
                                         name, val);
            t.remove(name);
            t.put(name, val);
        } catch (Exception e) {
            logger.debug("Error set config:" + module + category + name);
            return false;
        }
        return true;
    }

    public boolean setConfigStrings(String module, String category,
                                    String name, String[] val) {
        Map t = getConfigTable(module, category);
        try {
            BoxConfigUtil.setUserConfigValues(m_iID, module, category,
                                          name, val);
            t.remove(name);
            t.put(name, val);
        } catch (Exception e) {
            logger.debug("Error set config:" + module + category + name);
            return false;
        }
        return true;
    }

    public boolean setConfigInt(String module, String category,
                                String name, int val){
        Map t = getConfigTable(module, category);
        try {
            Integer tmp = new Integer(val);
            BoxConfigUtil.setUserConfigValue(m_iID, module, category,
                                         name, tmp.toString());
            t.remove(name);
            t.put(name, tmp.toString());
        } catch (Exception e) {
            logger.debug("Error set config:" + module + category + name);
            return false;
        }
        return true;
    }

    public boolean setConfigBoolean(String module, String category,
                                    String name, boolean val) {
        Map t = getConfigTable(module, category);
        try {
            String tmp = "0";
            if (val)
                tmp = "1";
            BoxConfigUtil.setUserConfigValue(m_iID, module, category,
                                         name, tmp);
            t.remove(name);
            t.put(name, tmp);
        } catch (Exception e) {
            logger.debug("Error set config:" + module + category + name);
            return false;
        }
        return true;
    }

    protected String getConfigValue(String module, String category,
                                    String name, boolean useDefault) {
        if (logger.isDebugEnabled()) {
            logger.debug("get user config:" + module+":"+category+":"+name);
        }

        String val = "";
        try {
            val = BoxConfigUtil.getUserConfigValue(m_iID, module,
                                               category, name);
        } catch (Exception e) {
            val = null;
        }

        if (val == null && useDefault) {
            ModuleConfig mod = (ModuleConfig)BoxStatic.mainConfig
                .getModule(module);
            if (logger.isDebugEnabled()) {
                logger.debug("Error getting user config:" + module+":"
                             +category+":"+name+ "=" + name);
            }
            val = mod.getConfigValue(category, name);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("value:" + val);
        }

        return val;
    }

    protected String[] getConfigValues(String module, String category,
                                       String name, boolean useDefault) {
        if (logger.isDebugEnabled()) {
            logger.debug("get user config:" + module+":"+category+":"+name);
        }

        String[] val = null;
        try {
            val = BoxConfigUtil.getUserConfigValues(m_iID, module,
                                                category, name);
        } catch (Exception e) {
            val = null;
        }

        if ( val == null && useDefault) {
            if (logger.isDebugEnabled()) {
                logger.debug("Error get user config:" + module+":"+category
                             +":"+name);
                logger.debug("Go get from module config:" + module+":"
                             +category+":"+name);
            }
            val = BoxStatic.mainConfig.getModule(module)
                .getConfigValues(category, name);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("value:" + val);
        }

        return val;
    }

    protected Map getConfigTable(String module, String category){
        Map moduleTable = (Map)m_settings.get(module);
        if (moduleTable == null) {
                moduleTable = new Hashtable(2);
                m_settings.put(module, moduleTable);
        }

        Map catTable = (Map)moduleTable.get(category);
        if (catTable == null) {
                catTable = new HashMap(2);
                moduleTable.put(category, catTable);
        }
        return catTable;
    }
}
